;;; test for strict-inequality constrained optimization problem
(load "strict-inequality-constrained-optimization.ss")

(define (example1)
  ;; f(v) = 1/2 (v0^2 + v1^2)
  ;; v0 = (300, 400),
  ;; constraint: c(v) := [(v0-150)^2 + (v1-200)^2)] - 125^2.
  (define (f v) (* 0.5 (+ (expt (vector-ref v 0) 2) (expt (vector-ref v 1) 2))))
  (define (grad v) v)
  (define v-init (vector 100 20))
  (define (constraints v)
    (list (+ (/ (expt (- (vector-ref v 0) 50) 2) (* 48 48))
             (/ (expt (vector-ref v 1) 2) 10000)
             -1)))
  (define (grad-constrs v)
    (list (vector (* 2 (- (vector-ref v 0) 50) (/ 1 (* 48 48)))
                  (* 2 (vector-ref v 1) 1/10000))))
  (list f grad constraints grad-constrs v-init))

(let* ([paras (append (example1) (list 1e-3 1e-3 1e-2 2e-2))]
       [sol-stream (apply SD-method-stream paras)])
  (for-each (lambda (j)
              (display (stream-ref sol-stream j)) (newline))
            (range 10)))


#|
|#


;;; 看来barrier方法有问题.

;; example2. Rosenbrock function
(define (rosenbrock)
  ;; f(v) = 100 (y-x^2)^2 + (1-x)^2; v=(x,y)=(1,1) is the solution.
  ;; constraint: c(v) := (x+0.5)^2 + (y-0.3)^2 -0.8  > 0
  (define (f v)
    (+ (expt (- 1 (vector-ref v 0)) 2)
       (* 100 (expt (- (vector-ref v 1) (expt (vector-ref v 0) 2)) 2))))
  (define (g v)
    (let ([aux (- (vector-ref v 1) (expt (vector-ref v 0) 2))])
      (vector (+ (* -2 (- 1 (vector-ref v 0)))
                 (* -400 (vector-ref v 0) aux))
              (* 200 aux))))
  (define v0 (vector -1.2 0))
  (define (constraints v)
    (list (+ (expt (+ (vector-ref v 0) 0.5) 2)
             (expt (- (vector-ref v 1) 0.3) 2)
             -0.8)))
  (define (grad-constrs v)
    (list (vector (* 2 (+ (vector-ref v 0) 0.5))
                  (* 2 (- (vector-ref v 1) 0.3)))))
  (list f g constraints grad-constrs v0))


#|
(let* ([paras (append (rosenbrock) (list 1e-3 1e-3 1e-2 2e-2))]
       [sol-stream (apply SD-method-stream paras)])
  (for-each (lambda (j)
              (display (stream-ref sol-stream j)) (newline))
            (range 5)))
|#


(exit)




